<?php
/*
<script type="text/javascript" src="<?php bloginfo('template_url');?>/admins/jquery.lazy-master/lazy.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/admins/jquery.lazy-master/jquery.lazy.js"></script>
*/


//fillter images in post content
//process add action wp_head
add_action('wp_head','db_head_lazy_loader');
function db_head_lazy_loader()
{
	echo '
	<script type="text/javascript" src="'.get_template_directory_uri().'/admins/jquery.lazy-master/lazy.js"></script>
	<script type="text/javascript" src="'.get_template_directory_uri().'/admins/jquery.lazy-master/jquery.lazy.js"></script>
		';	
}

// remove default width height, add class lazy, data-href in the_content
function addClass_images( $content ) {

	if(is_page('lien-he') || is_page('contact') || is_page('contact-us')){return $content;}
	else
	{
		$content = preg_replace( '/(width|height)="\d*"\s/', "", $content );
		$html = preg_replace_callback(
				'#(<img\s[^>]*src)="([^"]+)"#',
				"callback_img", $content );
		return $html;
	}
	
}
add_filter( 'the_content', 'addClass_images', 100 );
//add_filter( 'post_thumbnail_html', 'addClass_images', 100 );


function callback_img($match) {
	list(, $img, $src,$width,$height) = $match;
	$url = get_stylesheet_directory_uri().'/admins/jquery.lazy-master/loading_icon.gif';
	return "$img=\"$url\" class=\"lazy\" data-src=\"$src\" ";
}